package com.adcc.eoss.cawebservice.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.adcc.eoss.cawebservice.CAStringUtil;
import com.adcc.eoss.cawebservice.domain.CrewSchedule;
import com.adcc.eoss.cawebservice.domain.FlightInfo;
import com.adcc.eoss.cawebservice.domain.FlightPlanRepository;
import com.adcc.eoss.cawebservice.service.FlightPlanService;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class FlightPlanServiceImpl implements FlightPlanService {

	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private FlightPlanRepository repository;

	@Resource
	private Environment env;

	@Override
	public String getFlightPlan(String pilotId, String own, String std,
			String sta, String dep, String arr, String flt, String fdt,
			String short_reg, String beginDate, String endDate, String arn) {
		try {
			String result = "";
			ObjectMapper mapper = new ObjectMapper();
			HashMap<String, List<CrewSchedule>> map = new HashMap<String, List<CrewSchedule>>();
			map.put("result", repository.getFlightPlan(pilotId, own, std, sta,
					dep, arr, flt, fdt, short_reg, beginDate, endDate, arn));
			result = mapper.writeValueAsString(map);
			return result;
		} catch (Exception ex) {
			log.error(ex.getMessage());
			return "";
		}
	}

	@Override
	public String getFlightInfo(String flt, String fdt) {
		try {
			String result = "";
			Map<String, Object> urlVariables = new HashMap<String, Object>();
			urlVariables.put("p", flt + fdt + "$");
			result = CAStringUtil.getWebservice(
					env.getProperty("flight.info.url"), urlVariables);
			result = createInfoJson(result);
			return result;
		} catch (Exception ex) {
			log.error(ex.getMessage());
			return "";
		}
	}

	/**
	 * @param result
	 * @return
	 * @throws Exception
	 */
	private String createInfoJson(String result) throws Exception {
		String infoList[] = result.split("|");
		FlightInfo info = new FlightInfo();
		info.setFlightDate(infoList[0]);
		info.setFlightNo(infoList[1]);
		info.setAircraftNo(infoList[1]);
		info.setType(infoList[1]);
		info.setAdep(infoList[1]);
		info.setStd(infoList[1]);
		info.setDtd(infoList[1]);
		info.setOffTime(infoList[1]);
		info.setArr(infoList[1]);
		info.setSta(infoList[1]);
		info.setEta(infoList[1]);
		info.setLtd(infoList[1]);
		info.setState(infoList[1]);
		info.setAdeps(infoList[1]);
		info.setArrl(infoList[1]);
		info.setLockupTime(infoList[1]);
		info.setPushOutTime(infoList[1]);
		info.setSlipOffTime(infoList[1]);
		info.setPushInTime(infoList[1]);
		info.setOpenTime(infoList[1]);
		info.setLanding(infoList[1]);

		ObjectMapper mapper = new ObjectMapper();
		result = mapper.writeValueAsString(info).replace("null", "[]");
		return new String(result.getBytes("UTF-8"), "ISO_8859_1");
	}

}
